﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class Payable : BasePage
    {
        protected void ddlSupplier_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSupplier.SelectedIndex <= 0)
                LoadUnpaid(0);
            else
                LoadUnpaid(int.Parse(ddlSupplier.SelectedValue));


        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                LoadUnpaid(0);
                PaymentMethod methods = PaymentMethod.GetAllMethods();
                Utilities.LoadDropDownListNoSelect(ddlPaymentMethod, methods.DefaultView, PaymentMethod.ColumnNames.Name, PaymentMethod.ColumnNames.ID);
                methods.Filter = PaymentMethod.ColumnNames.Default + "=1 and " + PaymentMethod.ColumnNames.MethodType + "='P'";
                if (methods.RowCount > 0)
                    ddlPaymentMethod.SelectedValue = methods.ID.ToString();

                Suppliers sup = Suppliers.GetAllActiveSuppliers();
                Utilities.LoadDropDownList(ddlSupplier, sup.DefaultView, Suppliers.ColumnNames.SupplierName, Suppliers.ColumnNames.ID, "--All--");
            }

            btnPay.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
            btnPaymentHistory.Enabled = SessionUser.BranchID == Branches.CurrentBranchID;
        }

        public void btnSearch_OnClick(object sender, EventArgs e)
        {
            View_PurchaseOrders po = null;

            poList.SourceObject = po;

            RefreshList();
        }

        private void RefreshList()
        {
            poList.CurrentPageIndex = 0;
            poList.SortExpression = View_PurchaseOrders.ColumnNames.CreatedDate;
            poList.SortDirection = BaseListControl.SORT_DESC;

            poList.Refresh();

            decimal total = 0;
            decimal paid = 0;
            poList.SourceObject.Rewind();
            for (int i = 0; i < poList.SourceObject.RowCount; i++)
            {
                total += (decimal)poList.SourceObject.GetColumn("TotalAmount");
                paid += (decimal)poList.SourceObject.GetColumn("PaidAmount");
                poList.SourceObject.MoveNext();
            }

            lblTotal.Text = string.Format("{0:C}", total);
            lblPaid.Text = string.Format("{0:C}", paid);
            lblBal.Text = string.Format("{0:C}", total - paid);
        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            poList.ReadOnly = false;
            poList.Refresh();
        }

        private void LoadUnpaid(int id)
        {
            View_PurchaseOrders po = null;
            po = View_PurchaseOrders.GetUnpaidPOs(id);
            poList.SourceObject = po;

            RefreshList();
        }

        protected void btnPay_OnClick(object sender, EventArgs e)
        {
            poList.Pay(int.Parse(ddlPaymentMethod.SelectedValue));

            if(ddlSupplier.SelectedIndex <= 0)
                LoadUnpaid(0);
            else
                LoadUnpaid(int.Parse(ddlSupplier.SelectedValue));
        }
    }
}